package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.domain.Setmeal;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.SetmealService;
import com.itheima.utils.AliFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: SetmealController
 * @Author: Solomon
 * @Date: 2020/7/2 19:10
 * @Description:
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile){
        try {
            String newImgName = AliFileUtil.ByteUpload(imgFile);
            //文件上传成功，将名称存入redis集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newImgName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newImgName);
        } catch (IOException e) {
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //分页条件查询检查套餐
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findByPage(queryPageBean);
    }

    //新增套餐
    @RequestMapping("/add")
    public Result add(Integer[] checkGroupIds, @RequestBody Setmeal setmeal){
        try {
            setmealService.add(checkGroupIds,setmeal);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //编辑套餐时根据id查询套餐基本信息回显
    @RequestMapping("/findSetmealById")
    public Result findSetmealById(Integer setmealId){
        try {
            Setmeal setmeal = setmealService.findSetmealById(setmealId);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //检查套餐数据回显时，查询想关联的检查组的信息
    @RequestMapping("/findCheckGroupIdsById")
    public Result findCheckGroupIdsById(Integer setmealId){
        try {
            List<Integer> checkGroupIds = setmealService.findCheckGroupIdsById(setmealId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //修改检查套餐的基本信息和中间表的关联信息
    @RequestMapping("/update")
    public Result update(Integer[] checkGroupIds, @RequestBody Setmeal setmeal){
        try {
            setmealService.update(checkGroupIds,setmeal);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    //根据id删除检查套餐的信息
    @RequestMapping("/deleteById")
    public Result deleteById(Integer setmealId){
        try {
            setmealService.deleteById(setmealId);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
}
